//
//  RecPhotosController.m
//  CarFace
//
//  Created by dlios on 15/3/28.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "RecPhotosController.h"
#import "RecPhotoWallCell.h"

@interface RecPhotosController () 
@property(nonatomic, retain)NSMutableArray *listArr;

@property(nonatomic, retain)NSNumber *typeStr;
@end

@implementation RecPhotosController

static NSString * const reuseIdentifier = @"Cell";

- (void)dealloc
{
    [_typeStr release];
    [_idStr release];
    [_listArr release];
    [super dealloc];
}
- (void)handleData
{
    self.listArr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"%@", _idStr);
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:_idStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //        NSLog(@"%@", responseObject);
        NSError *error = nil;
        id request = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        //        NSLog(@"%@", request);
        //         NSLog(@"%@", [[request objectForKey:@"result"] objectForKey:@"list"]);
        NSArray *listArrays = [[request objectForKey:@"result"] objectForKey:@"list"];
        
        NSLog(@"------%@", [[request objectForKey:@"result"] valueForKey:@"index"]);
        [request objectForKey:@"result"] ;
        for (NSDictionary *dic in listArrays) {
            RecPhoto *rec = [RecPhoto objectWithDictionary:dic];
            [self.listArr addObject:rec];
        }
//        NSLog(@"%@", _listArr);
        
        [self.collectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self handleData];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    UICollectionViewFlowLayout *flow = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    flow.minimumInteritemSpacing = 0;
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.minimumLineSpacing = 0;
    flow.itemSize = self.view.bounds.size;
 
    // Register cell classes
    [self.collectionView registerClass:[RecPhotoWallCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self.collectionView setPagingEnabled:YES];
//    self.collectionView.contentOffset = CGPointMake(self.view.width * self.number, 0);
    
    // Do any additional setup after loading the view.
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:tap];
    [tap release];
    
    
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
//#warning Incomplete method implementation -- Return the number of sections
//    return 0;
//}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"jkljlkjkljl%ld", self.listArr.count);
    return (NSInteger)self.listArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RecPhotoWallCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor redColor];
    // Configure the cell
    cell.photo = self.listArr[indexPath.item];
//    NSLog(@"%@", [self.listArr[indexPath.item] imgurl]);
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
